package the.workshop.bu.controller;

import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wisteria.framework.util.DateUtils;
import org.wisteria.framework.web.APIResult;
import org.wisteria.framework.web.QueryResult;
import the.broccoli.et.application.service.EventTriggerService;
import the.broccoli.et.domain.model.UserEventStatistic;
import the.broccoli.et.interfaces.dto.EventTriggerCondition;
import the.broccoli.et.interfaces.dto.EventTriggerDTO;
import the.broccoli.uc.service.IUserService;
import the.broccoli.uc.service.impl.UserServiceImpl;
import the.workshop.bu.dto.CustomerQueryEventDTO;
import the.workshop.bu.dto.StatisticCustomerET;
import the.workshop.bu.dto.req.CustomerEtPage;
import the.workshop.bu.dto.req.CustomerEtQuery;
import the.workshop.bu.service.IStatisticService;
import the.workshop.bu.vo.StatisticProductVO;
import the.workshop.bu.vo.StatisticSalesmanVO;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/bu/statistic")
public class StatisticController {

    @Resource
    private IStatisticService statisticService;

    @Resource
    private EventTriggerService eventTriggerService;

    @Resource
    private IUserService userService;

    @GetMapping("/salesman")
    public APIResult<QueryResult<StatisticSalesmanVO>> salesman() {
        List<StatisticSalesmanVO> data = statisticService.salesman();
        return APIResult.success(new QueryResult<>(data.size(), data));
    }

    @GetMapping("/product")
    public APIResult<List<StatisticProductVO>> product() {
        return APIResult.success(statisticService.product());
    }

    @GetMapping("/customer-et")
    public APIResult<QueryResult<StatisticCustomerET>> customerET(CustomerEtQuery query) {
        List<UserEventStatistic> userEventStatisticList = eventTriggerService.listUserEventStatistic("queryCustomer-search", DateUtils.toLocalDateTime(query.getStartTime()), DateUtils.toLocalDateTime(query.getEndTime()));
        if (CollectionUtils.isEmpty(userEventStatisticList)) {
            return APIResult.success();
        }

        List<StatisticCustomerET> statisticCustomerETList = new ArrayList<>();
        StatisticCustomerET statisticCustomer = null;
        for (UserEventStatistic item : userEventStatisticList) {
            statisticCustomer = new StatisticCustomerET();
            statisticCustomer.setUserId(item.getUserId());
            statisticCustomer.setTotal(item.getTotal());
            statisticCustomer.setUserName(userService.getUserNameById(item.getUserId()));
            statisticCustomerETList.add(statisticCustomer);
        }

        return APIResult.success(new QueryResult<>(statisticCustomerETList.size(), statisticCustomerETList));
    }

    @GetMapping("/customer-event-detail")
    public APIResult<QueryResult<CustomerQueryEventDTO>> customerEtDetail(CustomerEtPage page) {
        EventTriggerCondition condition = new EventTriggerCondition();
        condition.setPageSize(page.getPageSize());
        condition.setPageNum(page.getPageNum());
        condition.setEventCode("queryCustomer-search");
        condition.setUserId(page.getUserId());
        condition.setStartTime(DateUtils.toLocalDateTime(page.getStartTime()));
        condition.setEndTime(DateUtils.toLocalDateTime(page.getEndTime()));
        QueryResult<EventTriggerDTO> result = eventTriggerService.pageList(condition);

        List<CustomerQueryEventDTO> items = null;
        if (!CollectionUtils.isEmpty(result.getItems())) {
            items = result.getItems().stream().map(this::toCustomerQueryEventDTO).collect(Collectors.toList());
        }

        return APIResult.success(new QueryResult<>(result.getTotal(), items));
    }

    private CustomerQueryEventDTO toCustomerQueryEventDTO(EventTriggerDTO eventTrigger) {
        CustomerQueryEventDTO dto = new CustomerQueryEventDTO();
        BeanUtils.copyProperties(eventTrigger, dto);
        dto.setUserName(userService.getUserNameById(eventTrigger.getUserId()));
        return dto;
    }
}
